import 'dart:typed_data';
import 'dart:ui';

import 'package:collection/collection.dart';
import 'package:example/datebase/text_data_dto.dart';
import 'package:flutter/material.dart';

///显示的画家
class LedViewPainter extends CustomPainter {
  LedViewPainter({
    required this.text,
    required this.canvasSize,
    required this.externalColor,
  });

  ///像素数组
  TextDataDTO text;

  ///画布大小
  Offset canvasSize;

  ///外部颜色
  Color externalColor;

  late int widthCount;
  late int heightCount;

  @override
  void paint(Canvas canvas, Size size) {
    widthCount = text.widthCount!;
    heightCount = 12;
    List<List<int>> pixels = setTextData(text.data!);
    // pixels = getConversionPixels(pixels);
    pixels = getConversionPixels(pixels);
    if (text == null) {
      return;
    }
    print("size.width ${size.width}  size.height ${size.height}  ");
    var rect = Rect.fromLTWH(0, 0, size.width, size.height);
    Paint paint = Paint();
    paint.shader = const LinearGradient(
      begin: Alignment.centerLeft,
      end: Alignment.centerRight,
      colors: [
        Color(0xFFFF0000),
        Color(0xFFFFFF00),
        Color(0xFF00FF00),
        Color(0xFF00FFFF),
        Color(0xFF0000FF),
        Color(0xFFFF00FF),
      ],
    ).createShader(rect);

    ///绘制底部的黑色背景
    // canvas.drawRect(rect, paint);
    ///图像文字外的颜色
    Paint rectPaint;
    rectPaint = Paint()
      ..color = Colors.black
      ..strokeWidth = 1
      ..style = PaintingStyle.stroke;
    Paint circlePaint = Paint()..color = Colors.yellow;

    ///循环矩阵数组-横向
    for (int i = 0; i < pixels.length; i++) {
      ///循环矩阵数组-纵向
      for (int j = 0; j < pixels[i].length; j++) {
        /////////////////////////////////////////////////////////////
        ///所有颜色透明度设为0.3

        ///矩形的大小
        var rectSize = size.height / heightCount;

        /////////////////////////画矩形
        canvas.drawRect(
          Rect.fromCenter(
              center: Offset(
                  i * rectSize + rectSize / 2, j * rectSize + rectSize / 2),
              width: rectSize,
              height: rectSize),
          rectPaint,
        );

        if (pixels[i][j] == 1) {
          continue;
        }
        ////////////////////////画圆
        canvas.drawRect(
            Rect.fromCenter(
                center: Offset(i.toDouble() * rectSize + rectSize * 5 / 10,
                    j.toDouble() * rectSize + rectSize * 5 / 10),
                width: rectSize * 10 / 10,
                height: rectSize * 10 / 10),
            circlePaint..color = externalColor);
      }
    }
  }

  @override
  List<List<int>> setTextData(Uint8List bArr) {
    // print("setTextData::::0   ${bArr}");
    int length = bArr.length ~/ 2;
    List<int> iArr = List.generate(length, (index) => 0);

    for (int i = 0; i < bArr.length / 2; i++) {
      int i2 = i * 2;
      int i3 = bArr[i2] & -1;
      int i4 = bArr[i2 + 1] & -1;
      int i5 = heightCount;
      iArr[i] = (i4 >> (16 - i5)) | (i3 << (i5 - 8));
    }
    List<List<int>> list1 = [];
    print("setTextData::::iArr   ${iArr}");
    for (int i6 = 0; i6 < widthCount; i6++) {
      // print("::::widthCount ${widthCount} ::::i6   ${i6}");
      List<int> list0 = [];
      for (int i7 = 0; i7 < heightCount; i7++) {
        int i8 = (iArr[i6] >> i7) & 1;
        // print("::::heightCount ${heightCount}  ::::i7   ${i7}  ::::i8   ${i8}");
        list0.add(i8);
      }
      list1.add(list0);
    }
    return list1;
  }

  List<List<int>> getConversionPixels(List<List<int>> pixels) {
    List<List<int>> list = [];
    pixels.forEachIndexed((index, element) {
      list.add(element
          .mapIndexed((index2, e) => element[element.length - 1 - index2])
          .toList());
    });
    return list;
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
